import 'taro-ui/dist/style/index.scss';
import '@nutui/nutui-react-taro/dist/style.css'
// import { getDvaApp } from 'umi';
import Taro, {useLaunch, useDidShow, useDidHide, getCurrentInstance, useRouter} from '@tarojs/taro';
import {Provider} from "react-redux";
import '../public/font/iconfont.css';
import './app.less';
import {wxLogin, authLocation} from "./utils";
import dva from "./dva"
import app from "./models/app";
import stations from "./models/stations";
import wallet from "./models/wallet";
import invoice from "./models/invoice";
import order from "./models/order";
import recharge from "./models/recharge";
import products from "./models/products";
import cars from "./models/cars";
import fleet from "./models/fleet";

const dvaApp = dva.createApp({
  initialState: {},
  models: [
    app,
    stations,
    wallet,
    invoice,
    order,
    recharge,
    products,
    cars,
    fleet
  ],
});
const store = dvaApp.getStore();

function App({children}) {
  useRouter((res) => console.log('useRouter', res));
  useDidShow((options) => {
    console.log('useDidShow', options);
    Taro.getNetworkType({
      success: function (res) {
        // 返回网络类型, 有效值：
        // wifi/2g/3g/4g/unknown(Android下不常见的网络类型)/none(无网络)
        // var networkType = res.networkType
        console.log('网络连接', res)
      }
    })
    let query = {};
    if (options.query && Object.keys(options.query).length) {
      query = {...options.query};
    } else if (Taro.getStorageSync('useDidShowQuery')) {
      const Q = JSON.parse(Taro.getStorageSync('useDidShowQuery'));
      query = {...Q};
    }
    Taro.setStorageSync('isScan', false);
    let paramStr = '';
    if (query && Object.keys(query).length) {
      // let pileStr = '';
      // let gunStr = '';
      // 普通二维码
      if (query.q) paramStr = unescape(query.q).split('code=').length > 1 ? unescape(query.q).split('code=')[1] : '';
      // 小程序码
      if (query.scene) paramStr = unescape(query.scene);
    }
    // const {query} = Taro.getLaunchOptionsSync();
    // console.log('const {query} = Taro.getLaunchOptionsSync();', Taro.getLaunchOptionsSync());
    // console.log('useDidShow', query);
    // console.log('useDidShowparamStr', paramStr);
    if (query && paramStr) {
      Taro.setStorageSync('useDidShowQuery', JSON.stringify(query))
      // const pileStr = unescape(options.query.scene).split('&').length > 1 ? unescape(options.query.scene).split('&')[0] : ''
      // const gunStr = unescape(options.query.scene).split('&').length > 1 ? unescape(options.query.scene).split('&')[1] : ''
      const pileStr = query.scene ? paramStr.split('&').length > 1 ? paramStr.split('&')[0] : '' : ''
      const gunStr = query.scene ? paramStr.split('&').length > 1 ? paramStr.split('&')[1] : '' : ''
      const pileCode = query.q ? paramStr.slice(0, -2) : pileStr.split('=').length >= 2 ? pileStr.split('=')[1] : '';
      const gunCode = query.q ? paramStr.slice(-2).startsWith('0') ? paramStr.slice(-1) : paramStr.slice(-2) :
        gunStr.split('=').length >= 2 ? gunStr.split('=')[1] : '';
      console.log('show==>扫码进入小程序pileCode', pileCode);
      console.log('show==>扫码进入小程序gunCode', gunCode);
      store.dispatch({
        type: 'stations/jumpToPile',
        payload: {
          pileCode,
          gunCode
          // pileId
        }
      });
      // setTimeout(() => Taro.reLaunch({url: '/moduleA/pages/pile/index'}), 1000)
    }
  });
  useLaunch((options) => {
    console.log('useLaunch', options);
    const upDateLocal = (obj) => {
      store.dispatch({
        type: 'app/upState',
        payload: {
          chooseLocationInStore: obj
        }
      });
      store.dispatch({
        type: 'stations/init',
        payload: {
          action: store.dispatch || undefined
        }
      });
    }
    wxLogin();
    authLocation(upDateLocal);
    Taro.setStorageSync('isScan', false);
    Taro.removeStorageSync('useDidShowQuery')
    let paramStr = '';
    if (options.query && Object.keys(options.query).length) {
      // let pileStr = '';
      // let gunStr = '';
      // 普通二维码
      if (options.query.q) paramStr = unescape(options.query.q).split('code=').length > 1 ? unescape(options.query.q).split('code=')[1] : '';
      // 小程序码
      if (options.query.scene) paramStr = unescape(options.query.scene);
    }
    if (options.query && options.query.q) {
      console.log('options.query.unescape', unescape(options.query.q));
    }
    // console.log('useLaunch', options.query);
    // console.log('useLaunchparamStr', paramStr);
    if (options.query && paramStr) {
      Taro.setStorageSync('useDidShowQuery', JSON.stringify(options.query))
      // const pileStr = unescape(options.query.scene).split('&').length > 1 ? unescape(options.query.scene).split('&')[0] : ''
      // const gunStr = unescape(options.query.scene).split('&').length > 1 ? unescape(options.query.scene).split('&')[1] : ''
      const pileStr = options.query.scene ? paramStr.split('&').length > 1 ? paramStr.split('&')[0] : '' : ''
      const gunStr = options.query.scene ? paramStr.split('&').length > 1 ? paramStr.split('&')[1] : '' : ''
      const pileCode = options.query.q ? paramStr.slice(0, -2) : pileStr.split('=').length >= 2 ? pileStr.split('=')[1] : '';
      const gunCode = options.query.q ? paramStr.slice(-2).startsWith('0') ? paramStr.slice(-1) : paramStr.slice(-2) :
        gunStr.split('=').length >= 2 ? gunStr.split('=')[1] : '';
      console.log('launch==>扫码进入小程序pileCode', pileCode);
      console.log('launch==>扫码进入小程序gunCode', gunCode);
      Taro.setStorageSync('isScan', true);
      store.dispatch({
        type: 'stations/jumpToPile',
        payload: {
          pileCode,
          gunCode
          // pileId
        }
      });
    }
  });
  useDidHide((hideOptions) => {
    const params = getCurrentInstance()?.router?.params || {}; // 路由参数
    console.log('路由参数', params);
    console.log('hideOptions', hideOptions);
  });
  // children 是将要会渲染的页面
  // return children
  return <Provider store={store}>{children}</Provider>;
}

export default App




